#ifndef _DynamicVB_H_
#define _DynamicVB_H_

template <class VertexType> class DynamicVB 
{
public:
  D3DVertexBuffer *m_pVB;

private :
    uint32 m_nVerts;
    uint32 m_nVertStride;
    uint32 m_nOffs;
    VertexType* m_pLockedData;
    int m_bLocked;
    int m_bReadWrite;










  public :

    uint32 GetBytesCount() const 
    { 
      return m_nVertStride * m_nVerts; 
    }
    uint32 GetVerts() const 
    { 
      return m_nVerts; 
    }
    int GetOffset() const 
    { 
      return m_nOffs; 
    }
    void Reset()
    {
      m_nOffs = m_nVerts;
    }
    int GetSize()
    {
      return _VertBufferSize(m_pVB);
    }

    DynamicVB()
    {
      m_pVB = 0;

      m_bLocked = false;
      m_pLockedData = NULL;






      m_nOffs = 0;
      m_bReadWrite = false;
      m_nVertStride = sizeof(VertexType);
      m_nVerts = 0;
    }

    DynamicVB(D3DDevice *pD3D, const unsigned int& theVertsCount, bool bReadWrite)
    {
      m_pVB = 0;

      m_bLocked = false;
      m_pLockedData = NULL;






      m_nOffs = 0;
      m_bReadWrite = bReadWrite;
      m_nVertStride = sizeof(VertexType);
      m_nVerts = theVertsCount;

      if (m_nVertStride > 1)
        CreateVB(pD3D, theVertsCount, m_nVertStride);
    }

    HRESULT CreateVB(D3DDevice *pD3D, uint32 nVerts, uint32 nStride)
    {
      m_nVertStride = nStride;
      m_nVerts = nVerts;

      HRESULT hr = S_OK;
			int32 nUsage; 
			nUsage = CDeviceManager::USAGE_CPU_WRITE;
			nUsage |= ((CDeviceManager::USAGE_CPU_READ|CDeviceManager::USAGE_STAGING) & -m_bReadWrite) | ((CDeviceManager::USAGE_DYNAMIC) & ~m_bReadWrite);
			hr = gRenDev->m_DevMan.CreateBuffer(
				m_nVerts, m_nVertStride, nUsage, CDeviceManager::BIND_VERTEX_BUFFER, (D3DBuffer**)&m_pVB);






			CHECK_HRESULT(hr);
      return hr;
    }

    VertexType* LockVB(const uint32 theLockVertsCount, uint32 &nOffs, bool bWrite=true, bool bNonStalling=false);
    void UnlockVB();



    
		HRESULT Bind(uint32 StreamNumber=0, int nBytesOffset=0, int nStride=0, uint32 nFreq=1);

    void Release()
    {
      UnlockVB();








      SAFE_RELEASE(m_pVB);
    }

    ~DynamicVB()
    {
      Release();
    }
  
};

#endif  _DynamicVB_H_
